/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.*;
import POJOS.Playlist;
import DB.DBmanager;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import GUI.SongsList;
/**
 *
 * @author john
 */
public class NewOrModifyPlayListForm extends javax.swing.JFrame {
    Song song;
    Playlist playlist;
    boolean modify;
    int confirm;
    int selectedRow;
    /**
     * Creates new form NewOrModifyPlayListForm
     */
    public NewOrModifyPlayListForm() {
        initComponents();
        checkButtons();
        modify = false;
        songList.clear();
    }
    
    private void checkButtons(){
        selectedRow = songsListTable.getSelectedRow();
        delete_songs_btn.setEnabled(selectedRow>=0);
    }
    
    public NewOrModifyPlayListForm(Playlist pl){
        initComponents();
        checkButtons();
        songsListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        modify = true;
        
        playlist = pl;
        
        setTitle("ΤΡΟΠΟΠΟΙΗΣΗ ΛΙΣΤΑΣ");
        jLabel1.setText("Παρακαλώ τροποποιήστε την λίστα: ");
        exit_songs_btn.setText("ΑΚΥΡΩΣΗ");
        
        listName.setText(playlist.getNamePlaylist());
        list_description.setText(playlist.getDescription());
        plistformationDate.setDate(playlist.getCreationDate());
        
        songList.clear();
        
        RadioStationPUEntityManager.getTransaction().begin();
        playlist = RadioStationPUEntityManager.merge(playlist);
        RadioStationPUEntityManager.getTransaction().commit();
        
        for (Song s : playlist.getSongCollection()){
            songList.add(s);
        }
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        songQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT s FROM Song s");
        songList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : songQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        list_description = new javax.swing.JTextField();
        input_songs_btn = new javax.swing.JButton();
        delete_songs_btn = new javax.swing.JButton();
        exit_songs_btn = new javax.swing.JButton();
        save_songs_btn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        songsListTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        listName = new javax.swing.JTextField();
        plistformationDate = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Φόρμα Διαχείρισης Λίστας");

        jLabel2.setText("ΠΕΡΙΓΡΑΦΗ:");

        jLabel3.setText("ΗΜΕΡΟΜΗΝΙΑ ΔΗΙΜΙΟΥΡΓΙΑΣ:");

        jLabel4.setText("ΛΙΣΤΑ ΤΡΑΓΟΥΔΙΩΝ:");

        list_description.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                list_descriptionActionPerformed(evt);
            }
        });

        input_songs_btn.setText("ΕΙΣΑΓΩΓΗ ΤΡΑΓΟΥΔΙΟΥ");
        input_songs_btn.setPreferredSize(new java.awt.Dimension(100, 25));
        input_songs_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                input_songs_btnActionPerformed(evt);
            }
        });

        delete_songs_btn.setText("ΔΙΑΓΡΑΦΗ ΤΡΑΓΟΥΔΙΟΥ");
        delete_songs_btn.setPreferredSize(new java.awt.Dimension(100, 25));
        delete_songs_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_songs_btnActionPerformed(evt);
            }
        });

        exit_songs_btn.setText("ΑΚΥΡΩΣΗ");
        exit_songs_btn.setPreferredSize(new java.awt.Dimension(100, 25));
        exit_songs_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_songs_btnActionPerformed(evt);
            }
        });

        save_songs_btn.setText("ΑΠΟΘΗΚΕΥΣΗ");
        save_songs_btn.setPreferredSize(new java.awt.Dimension(100, 25));
        save_songs_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_songs_btnActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, songList, songsListTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Title");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAlbum.idArtist.artisticName}"));
        columnBinding.setColumnName("Artist");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAlbum.idMusicGroup.nameMusicGroup}"));
        columnBinding.setColumnName("Music Group");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Duration");
        columnBinding.setColumnClass(Float.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        songsListTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                songsListTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(songsListTable);

        jLabel5.setText("ΟΝΟΜΑ:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(input_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(delete_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(save_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exit_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(list_description, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(listName)
                            .addComponent(plistformationDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(listName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(list_description, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(plistformationDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(20, 20, 20)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(input_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(delete_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(save_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exit_songs_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(115, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void list_descriptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_list_descriptionActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_list_descriptionActionPerformed

    private void input_songs_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_input_songs_btnActionPerformed
        //selectedRow = -1;
        //new SongsList().setVisible(true);
        if (modify == true) {
            new SongsList(playlist).setVisible(true);    
        }
        else {
            new SongsList().setVisible(true);
        }
    }//GEN-LAST:event_input_songs_btnActionPerformed

    private void exit_songs_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exit_songs_btnActionPerformed
        dispose();
    }//GEN-LAST:event_exit_songs_btnActionPerformed

    private void save_songs_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_songs_btnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_save_songs_btnActionPerformed

    private void delete_songs_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_songs_btnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_delete_songs_btnActionPerformed

    private void songsListTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_songsListTableMouseClicked
        checkButtons();
    }//GEN-LAST:event_songsListTableMouseClicked
    
    /*private void songsListTableMouseClicked(java.awt.event.MouseEvent evt) {                                          
        // TODO add your handling code here:
        checkButtons(); 
    }*/   
    
    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
        // TODO add your handling code here:
        //Με κλικ στο υπόλοιπο GUI εκτός πίνακα ενεργοποιείται η εισαγωγή καλλιτέχνη
        selectedRow = -1;
        delete_songs_btn.setEnabled(selectedRow>=0);
    }           
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyPlayListForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyPlayListForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyPlayListForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyPlayListForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewOrModifyPlayListForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager;
    private javax.swing.JButton delete_songs_btn;
    private javax.swing.JButton exit_songs_btn;
    private javax.swing.JButton input_songs_btn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField listName;
    private javax.swing.JTextField list_description;
    public static org.jdesktop.swingx.JXDatePicker plistformationDate;
    private javax.swing.JButton save_songs_btn;
    public static java.util.List<POJOS.Song> songList;
    private javax.persistence.Query songQuery;
    private javax.swing.JTable songsListTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
